/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proy2redes;

/**
 *
 * @author jose
 */
import java.net.*;
import java.io.*;
import java.util.*;

public class drep{
    private static String HOSTDMON = "localhost";
    private static String PUERTODMON = "39495";
    private static String RDIRECTORIO = ".";
    //private static String ARCHIVO = null;
    private static String PUERTOLOCALCLIENTES = "31109";

// private int puertoLocalClientes;
// private String hostdmon;
// private int puertodmon;
// private String directorio;

   public static HashMap organizarEntrada(String[] args){
        HashMap map = new HashMap();

        int tam = args.length;
        System.out.println("OrganizarEntrada " +tam);
        if (tam%2!=0) {
            return null;
        }
        int i = 0;
        while (i<tam){
            map.put(args[i], args[i+1]);
            i = i+2;
        }
        System.out.println("Tamano del Map: "+map.size());
        return map;
    }

    public static boolean comprobarEntrada(String[] args,String[] values){
        boolean valido = false;
        HashMap map = organizarEntrada(args);
        if (map==null){
            return false;
        }
        
        if (map.containsKey("-h")){
            values[0] = (String)map.get("-h");
            map.remove("-h");
        }
        if (map.containsKey("-p")){
            values[1] = (String)map.get("-p");
            map.remove("-p");
        }
        if (map.containsKey("-d")){
            values[2] = (String)map.get("-d");
            map.remove("-d");
        }
        if (map.containsKey("-c")){
            values[3] = (String)map.get("-c");
            map.remove("-c");
        }
        
        if (map.isEmpty())
            valido = true;

        return valido;
    }

  public static void main(String[] args) throws IOException {
    String[] valores = new String[4];
    valores[0] = HOSTDMON;
    valores[1] = PUERTODMON;
    valores[2] = RDIRECTORIO;
    valores[3] = PUERTOLOCALCLIENTES;

    // Canal de entrada para el cliente
    BufferedReader inDmon = null;

    // Canal de salida hacia el servidor Dmon
    PrintWriter outDmon = null;

    boolean b = comprobarEntrada(args,valores);

    if (!b) {
      System.out.println("Error en los parametros de entrada");
      return;
    }
    String hostdmon = valores[0];
    int portdmon = Integer.parseInt(valores[1]);
    String rdirect = valores[2];rdirect = "/home/jose/Documentos/pjava";//Quitar
    int portlocal = Integer.parseInt(valores[3]);
    //String name = valores[4];
    ServerSocket serverSocket = null;
    Socket socketDmon = null;
    boolean listening = true;
    //String rdirect = "/home/jose/Documentos/pjava";

    try {
      serverSocket = new ServerSocket(portlocal);
    } catch (IOException e) {
      System.out.println("No puede escuchar en el puerto "+portlocal);
      System.exit(-1);
    }
    /*try {
      socketDmon = new Socket(hostdmon,portdmon);
      inDmon = new BufferedReader(new InputStreamReader(socketDmon.getInputStream()));
      outDmon = new PrintWriter(socketDmon.getOutputStream(), true);
    } catch (IOException e) {
      System.out.println(e);
      System.exit(-1);
    }*/
    drepIO hiloIO = new drepIO(rdirect);
    hiloIO.start();
    while (hiloIO.isAlive()) {
      drepH hilo = new drepH(serverSocket.accept());
      System.out.println("Crea un hijo");
      hilo.inicValores(rdirect,hostdmon,portdmon);
      //hilo.inicValoresDmon(inDmon, outDmon);
      hilo.start();
    }
    serverSocket.close();
  }
}
